﻿using System.Linq;
using System.Text;
using Dogshow.BusinessLogic.Managers;
using Dogshow.BusinessLogic.Services;
using Dogshow.BusinessLogic.Utils;
using Dogshow.Infrastructure.Exceptions;
using Dogshow.Model;
using Dogshow.Web.Models;
using DogshowRepository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Dogshow.BusinessLogicTests.Managers
{
    [TestClass]
    public class RegistracijaNalogaManagerFixtures
    {
        private RegistracijaNalogaManager m_RegistracijaNalogaManager;

        private Mock<IKorisniciRepository> m_KorisniciRepository;

        private Mock<IKinoloziRepository> m_KinoloziRepository;

        private Mock<IMestaRepository> m_MestaRepository;

        private Mock<IEncryptor> m_Encryptor;

        private Mock<INotificationService> m_NotificationService;

        [TestInitialize]
        public void TestInit()
        {
            m_Encryptor = new Mock<IEncryptor>();
            m_KinoloziRepository = new Mock<IKinoloziRepository>();
            m_KorisniciRepository = new Mock<IKorisniciRepository>();
            m_MestaRepository = new Mock<IMestaRepository>();
            m_NotificationService = new Mock<INotificationService>();

            m_RegistracijaNalogaManager = new RegistracijaNalogaManager(m_Encryptor.Object, m_KorisniciRepository.Object,
                                                                        m_MestaRepository.Object, m_KinoloziRepository.Object, m_NotificationService.Object);
        }

        [TestMethod][ExpectedException(typeof(KorisnikPostojiException))]
        public void Registruj_Korisnika_Kada_Je_Koriscnicko_Ime_Zauzeto()
        {
            // Arrange
            m_KorisniciRepository.Setup(x => x.PostojiKorisnik("zeks")).Returns(true);

            // Act
            m_RegistracijaNalogaManager.RegistrujKorisnika("zeks", null, null, null, null, null, null);
        }

        [TestMethod]
        public void Registruj_Korisnika_Sa_Postojecim_Mestom()
        {
            // Arrange
            m_MestaRepository.Setup(x => x.VratiPoPostanskomBroju("21000")).Returns(new Mesto
                                                                                        {
                                                                                            Id = 1,
                                                                                            PostanskiBroj = "21000",
                                                                                            Naziv = "Novi Sad"
                                                                                        });
            m_KinoloziRepository.Setup(x => x.Sacuvaj(It.IsAny<Kinolog>())).Returns(1);

            const string korisnickoIme = "zeks";
            const string mesto = "(21000) Novi Sad";
            const string imeIPrezime = "Željko Bajšanski";
            const string lozinka = "1302";
            const string adresa = "Preradovićeva 36";
            const string telefon = "063/528 065";
            const string email = "zeljko.bajsanski@gmail.com";

            m_KorisniciRepository.Setup(x => x.PostojiKorisnik("zeks")).Returns(false);

            var encryptedPassword  = Encoding.UTF8.GetBytes(lozinka);
            m_Encryptor.Setup(x => x.Encrypt(lozinka)).Returns(encryptedPassword);
            
            // Act
            m_RegistracijaNalogaManager.RegistrujKorisnika(korisnickoIme, imeIPrezime, lozinka, adresa, mesto, telefon, email);

            // Assert
            m_KorisniciRepository.Verify(x => x.Sacuvaj(It.Is<Korisnik>(k => k.KorisnickoIme == korisnickoIme
                                                                             && k.Naziv == imeIPrezime
                                                                             && k.Lozinka.SequenceEqual(encryptedPassword)
                                                                             && k.Adresa == adresa
                                                                             && k.Telefon == telefon
                                                                             && k.Email == email
                                                                             && k.VrstaKorisnika == VrsteKorisnika.Korisnik
                                                                             && k.IdKinologa == 1)));
        }
    }
}